const express = require("express")
const router = express.Router()
const adminServ = require("../../services/adminService")
const {
    asyncHandler
} = require("../getSendResult");
const a = require("../../util/crypt");
const {
    getResult
}=require("../getSendResult")
const jm=require("../../util/crypt")
router.post("/login", asyncHandler(async (req, res, next) => {
    // login用户登录
    const result = await adminServ.login(req.body.loginId, req.body.loginPwd)
    console.log(result)
    if (result) {
        // 针对其他终端
        let value = result.id;
        value = a.encrypt(value.toString()) //使用自己写的加密方法
        res.cookie("token", value, {
            path: "/",
            domain: "localhost",
            maxAge: 7 * 24 * 3600 * 1000, //毫秒
            // signed: true //使用秘钥加密
        })
        res.header("authorization", value);
    }
    return result
}))

router.post("/register",asyncHandler(async(req,res,next)=>{
    console.log(req.body)
    return await adminServ.addAdmin(req.body)
}))

router.post("/find",asyncHandler(async(req,res,next)=>{
   if(req.body.id){
    const result= jm.decrypt(req.body.id)
    return await adminServ.getAdminId(result)
  }else{
  return  getResult("no token！")
  }
}))

router.post("/update",asyncHandler(async(req,res,next)=>{
    const id=req.body.id
    const img=req.body.img
    console.log(req.body)
    return await adminServ.updateimg(id,{
       img:req.body.img
    })
}))

module.exports = router